package com.dushibao.dao;

import cn.hutool.core.util.ObjectUtil;
import com.dushibao.common.Constants;
import com.dushibao.entity.GoodsPhoto;

import com.dushibao.utils.DBUtils;
import com.dushibao.utils.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsPhotoDao {

    public Integer save(GoodsPhoto goodsPhoto) {

        try{
            String sql = "insert into dushibao_goods_photo(goods_id,photo_id) values(?,?)";

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,goodsPhoto.getGoodsId());
            stmt.setObject(2,goodsPhoto.getPhotoId());

            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public GoodsPhoto getById(long id) {
        try{
            String sql = "select goods_id,photo_id from dushibao_goods_photo where goods_id=? ";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setLong(1,id);
            ResultSet rs = stmt.executeQuery();
            GoodsPhoto goodsPhoto = new GoodsPhoto();

            if(rs.next()){
                goodsPhoto.setGoodsId(rs.getLong("goods_id"));
                goodsPhoto.setPhotoId(rs.getLong("photo_id"));
            }

            return goodsPhoto;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public Integer deleteById(List<Long> ids) {
        try{

            StringBuilder sql = new StringBuilder(" delete from dushibao_goods_photo where goods_id in ( ");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));

                if(i<ids.size()-1){
                    sql.append(",");
                }

            }
            sql.append(")");

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
